package com.andan.playerpos.listener;

import com.andan.playerpos.Playerpos;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.HashMap;
import java.util.Set;

public class Playerlistene implements Listener {
    HashMap<Player, Location> plist = new HashMap<>();
    Playerpos pl = Playerpos.getInstance();

    @EventHandler
    private void onInt(PlayerInteractEvent e) {
        if (e.getItem() != null && e.getItem().equals(Playerpos.getInstance().getConfig().getItemStack("setting.itemStack"))) {
            Player player = e.getPlayer();
            e.setCancelled(true);
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (player.isSneaking()) {
//Shift右键点击
                    opengui(player);


                } else {
//右键点击
                    plist.put(player, player.getLocation());
                    player.sendMessage(pl.getConfig().getString("message.addname"));
                    pl.getServer().getScheduler().runTaskLater(pl, () -> {
                        if (plist.containsKey(player))
                            plist.remove(player);
                    }, 20 * pl.getConfig().getInt("setting.time"));

                }


            }
            if (e.getAction() == Action.RIGHT_CLICK_AIR) {
                if (player.isSneaking())
                    opengui(player);
            }
        }
    }

    @EventHandler
    private void onClick(InventoryClickEvent e) {
        if (e.getClickedInventory() == null) return;
        if (!e.getView().getTopInventory().getTitle().equals(pl.getConfig().getString("setting.title"))) return;
        e.setCancelled(true);
        if (e.getCurrentItem() == null) return;
        String displayName = e.getCurrentItem().getItemMeta().getDisplayName().replace("§a", "");
        e.getWhoClicked().closeInventory();
        e.getWhoClicked().teleport((Location) pl.getConfig().get("data." + e.getWhoClicked().getName() + "." + displayName));

    }


    private void opengui(Player player) {
        Inventory inv = Bukkit.createInventory(null, 54, pl.getConfig().getString("setting.title"));
        Set<String> keys = pl.getConfig().getConfigurationSection("data." + player.getName()).getKeys(false);
        for (String k : keys) {
            inv.addItem(getitem(k));
        }
        player.openInventory(inv);

    }

    private ItemStack getitem(String k) {
        ItemStack is = new ItemStack(Material.ENDER_PEARL);
        ItemMeta itemMeta = is.getItemMeta();
        itemMeta.setDisplayName("§a" + k);
        is.setItemMeta(itemMeta);
        return is;
    }


    @EventHandler
    private void onchat(AsyncPlayerChatEvent e) {
        if (!plist.containsKey(e.getPlayer())) return;
        e.setCancelled(true);
        pl.getConfig().set("data." + e.getPlayer().getName() + "." + e.getMessage().replace(".",""), plist.get(e.getPlayer()));
        pl.saveConfig();
        e.getPlayer().sendMessage(pl.getConfig().getString("message.success"));
        plist.remove(e.getPlayer());
    }


}
